import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Warehouse } from 'src/entity/Warehouse'
import { IsNull, Repository } from 'typeorm'

@Injectable()
export class WarehouseService {
  constructor(
    @InjectRepository(Warehouse)
    private warehouseRepository: Repository<Warehouse>,
  ) { }
  async add(data) {
    return await this.warehouseRepository.save(data)
  }

  async del(id: number) {
    return await this.warehouseRepository.update(
      {
        id: id,
      },
      {
        isDelete: true,
      },
    )
  }

  async update(data) {
    return await this.warehouseRepository.update(
      {
        id: data.id,
      },
      data,
    )
  }

  // 更新某个属性
  async updateProperty(id, property, value) {
    return await this.warehouseRepository.update(
      {
        id: id,
      },
      {
        [property]: value,
      },
    )
  }

  // 分页查询
  async getPageList({ pageRequest: { pageIndex, pageSize } }) {
    const sqlAllData = await this.warehouseRepository.find({
      where: {
        isDelete: IsNull(),
      },
    })
    const sqlData = await this.warehouseRepository.find({
      where: {
        isDelete: IsNull(),
      },
      skip: (pageIndex - 1) * pageSize,
      take: pageSize,
      withDeleted: false,
    })

    return {
      content: sqlData,
      total: sqlAllData.length,
    }
  }

  // 获取仓库列表
  async getWarehouseList() {
    const sqlDataList = await this.warehouseRepository.find({
      where: {
        isDelete: IsNull(),
      },
    })
    return sqlDataList.map((item) => {
      return {
        label: item.warehouseName,
        value: item.id.toString(),
      }
    })
  }
}
